{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 对连续型特征，可以用哪个函数可视化其分布？（给出你最常用的一个即可），并根据代码运行结果给出示例。（10分） \n",
    "可用hist函数可视化其分布。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAQmElEQVR4nO3df6zdd13H8efL1m0yZZOumLkOWrIFKSgDrwMEjW4CXYIU44idGhczM40sgkq0M8G4Jf4xY6wmTOPCZpZF2eaE2CA6CcNEiZbdspFRZsNlTFaK0GVjZBA2Cm//ON/Sw/Ge3u/t/XHO+dznI7np+X6/n3PP+3bf89rnvs/3+2mqCklSu75r0gVIktaWQS9JjTPoJalxBr0kNc6gl6TGbZ50AaPOO++82r59+6TLkKSZcvDgwcerautix6Yu6Ldv3878/Pyky5CkmZLkf8Yds3UjSY0z6CWpcQa9JDXOoJekxhn0ktQ4g16SGmfQS1LjDHpJapxBL0mNm7o7Y6Wplpx87D/aoxnhjF6SGmfQS1LjbN2ofcPtFrDlog3HGb0kNc6gl6TGGfSS1Dh79NJq8LJLTTFn9JLUOINekhpn0EtS4wx6SWqcQS9JjTPoJalxXl4prRcvwdSEOKOXpMYZ9JLUOINekhpnj16zwf62dNp6zeiT7EpyOMlCkr2LHD8zyV3d8QNJto8cf0GSp5O8c3XKliT1tWTQJ9kE3AxcAewErkqyc2TYNcCTVXURsA+4aeT4PuCfV16uJGm5+szoLwUWquqRqnoWuBPYPTJmN3B79/ge4PJk8Lt2krcAjwCHVqdkSdJy9An6C4DHhraPdPsWHVNVx4GngC1JzgZ+H7jhVC+Q5Nok80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GrW9V/SR5E/Clqjo4vHuRoVNZf+e1VfVKBqu1vi3JT066oGXYDLwS+KuqegXwVaa0zXEqXQ/7zcDfT7qW5eg+O9gN7AB+EDibwXk0at3P/yaDPsl3Mwj5v62q93W7v5jk/O74+QxmzFOrqr4M/BuDzxnO7ZaWgOleRuK1wJuTPMpgldPLGMzwZ6V+qupo9+eXGPSHL2V2zp0jwJGqOtBt38Mg+Gel/hOuAD5eVV/stmel/p8BPltVx6rqG8D7gB9nCs7/5oK+Wx75VuDhqvqzoUPDyzRczaB3P1WSbE1ybvf4exicOA8DH2GwtARMae0AVXV9VW2rqu0MfvW+r6p+iRmpP8nZSb7vxGMGfeJPMgPnDkBV/S/wWJIXd7suZ3BX+kzUP+QqTrZtYHbq/xzw6iTP6XLoxN//xM//5u6MTfI64N+BhzjZJ/4DBn36u4EXMPgP8taqemIiRY6R5EcYrOu/icH/hO+uqhuTvIjBDPl5wAPAL1fVM5OrdGlJfgp4Z1W9aVbq7+p8f7e5Gfi7qvrjJFuY8nPnhCSXAO8BzgAeAX6V7lxiNup/DoMPNF9UVU91+2bp7/8G4BcYXP33APBrDHryEz3/mwt6SdJ3aq51I0n6Tga9JDXOoJekxhn0ktQ4g16SGmfQS1LjDHpJatz/AcXXZ4fJ4+oAAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    " \n",
    "mu = 50  # 均值\n",
    "sigma = 10  # 方差\n",
    "x = mu + sigma * np.random.randn(2000)\n",
    " \n",
    "plt.hist(x, bins=100,color='red',density=True)#绘制直方图函数\n",
    " \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 对两个连续型特征，可以用哪个函数得到这两个特征之间的相关性？根据代码运行结果，给出示例。（10分） \n",
    "可以使用pandas函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>A</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.022297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>B</td>\n",
       "      <td>-0.022297</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B\n",
       "A  1.000000 -0.022297\n",
       "B -0.022297  1.000000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "mu_x = 50  # 均值\n",
    "sigma_x = 10  # 方差\n",
    "mu_y =40  # 均值\n",
    "sigma_y = 8  # 方差\n",
    "x = mu + sigma * np.random.randn(2000)\n",
    "y = mu + sigma * np.random.randn(2000)\n",
    "data = pd.DataFrame({'A':x, \n",
    "                     'B':y})\n",
    "data.corr() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 如果发现特征之间有较强的相关性，在选择线性回归模型时应该采取什么措施。（10分） \n",
    "\n",
    "如果特征之间有较强的相关性，会导致冗余，模型复杂度继续增加，造成过拟合现象，可通过在目标函数中增加正则项减弱过拟合现象。\n",
    " L2正则使得线性回归系数收缩，模型稳定。\n",
    "• 当输入特征之间存在共线性时使用L2正则。\n",
    "• L1正则也会收缩回归系数。当正则参数取合适值时，L1正则使得有些线性回归系数为0，得到稀疏模型。\n",
    "• 当输入特征多，有些特征与目标变量之间相关性很弱时， L1正则可能只选择强相关的特征，模型解释性好。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 当采用带正则的模型以及采用随机梯度下降优化算法时，需要对输入（连续型）特征进行去量纲预处理。课程代码给出了用标准化（StandardScaler）的结果，请改成最小最大缩放（MinMaxScaler）去量纲 （10分），并重新训练最小二乘线性回归、岭回归、和Lasso模型（30分）。\n",
    "\n",
    "最小最大缩放（MinMaxScaler）去量纲 在jichuzuoye_4.ipyn文件中进行了体现,进一步地，重新训练最小二乘线性回归、岭回归、和Lasso模型在jichuzuoye_4_2.ipyn文件中进行了体现。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 代码中给出了岭回归（RidgeCV）和Lasso（LassoCV）超参数（alpha_）调优的过程，请结合两个最佳模型以及最小二乘线性回归模型的结果，给出什么场合应该用岭回归，什么场合用Lasso，什么场合用最小二乘。（30分）\n",
    " \n",
    "最小二乘法：基于均方误差最小化来进行模型求解的方法.\n",
    "岭回归：在“最小二乘法”的基础上引入L2范数正则化.\n",
    "LASSO：在“最小二乘法”的基础上引入L1范数正则化.\n",
    "结合两个最佳模型以及最小二乘线性回归模型的结果，当样本特征很多，而样本数相对较少时，容易陷入过拟合，可考虑使用岭回归或LASSO回归，LASSO更容易获得稀疏解，即求得的w会有更少的非零分量。如果没有明显的过拟合问题，可以考虑使用最小二乘。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
